import { ESSceneObject, ESTerrainLayer } from "earthsdk3";

export function getCzmCodeFromESTerrainLayer(czmTerrain: ESTerrainLayer) {
    if (!czmTerrain.url) return undefined;
    return /*js*/`
    var terrainProvider = new Cesium.CesiumTerrainProvider({
        url: ${getFinalTerrainProviderUrlString(typeof czmTerrain.url == 'object' ? czmTerrain.url.url : czmTerrain.url)},
    });
    viewer.terrainProvider = terrainProvider;
    viewer.camera.flyTo({
        destination: Cesium.Rectangle.fromDegrees(west, south, east, north)
    });`;
}
export function getFinalTerrainProviderUrlString(url: string) {
    let finalUrl;
    if (url.startsWith('ion://')) {
        const idStr = url.substring('ion://'.length);
        const id = +idStr;
        if (Number.isFinite(id)) {
            finalUrl = `Cesium.IonResource.fromAssetId(${id})`;
        }
    } else {
        finalUrl = `'${ESSceneObject.context.getStrFromEnv(url)}'`;
    }
    return finalUrl;
}